
import numpy as np
import csv
import pandas as pd
from datetime import datetime,timedelta
import re
import netCDF4 as nc

def extract_date_from_filename(file_path):
    """
    Extract date part from file path.

    Parameters:
        file_path (str): File path.

    Returns:
        str: Extracted date part in YYYYMMDD format.
    """
    match = re.search(r"_d(\d{8})_", file_path)
    if match:
        return match.group(1)
    return "unknown_date"
def sepc_file(file_path):
    Solar=open(file_path,"r")
    text=Solar.readlines()
    # Start using from index 16
    # Missing Dst and Xray
    text=text[16:]
    # Remove the introductory part at the beginning of the text
    KP=text[0][40:43]
    Kp=KP.split()
    envlist=list()
    date_time_list = []
    # start_date=datetime.strptime(start_time, "%Y%m%d%H%M")
    # time_interval=timedelta(hours=1)
    # # Initialize a list to save all date and time strings
    # date_time_list = []
    # end_date=datetime.strptime(end_time, "%Y%m%d%H%M")
    # # Generate date and time strings
    # current_date = start_date
    # while current_date <= end_date:
    #     date_time_str = current_date.strftime("%Y-%m-%dT%H:%M:%S")
    #     date_time_list.append(date_time_str)
    #     current_date += time_interval
    i=0
    for j in range(24):
        F10_7=int(text[i][17:20])
        SSN=int(text[i][25:28])
        AP=int(text[i][40:43])
        KP=text[i][57:114]
        Kp=KP.split()
        if len(Kp)<8:
            Kp_3h=2
        else:
            Kp_3h=int(Kp[-int((j)/3)])
        ACE=text[i][193:200]
        wind=ACE.split('-')
        wind_low=int(wind[0])
        wind_high=int(wind[1])
        C=int(text[i][209:211])
        M=int(text[i][217:219])
        X=int(text[i][225:227])
        envlist.append([F10_7,SSN,AP,Kp_3h,wind_low,wind_high,C,M,X])
    # envlist is the obtained subsequent values
    envlist=np.array(envlist)
    # Reverse order (after reverse, the first row is start_time, the last row is end_time)
    envlist1=envlist[::-1,:]
    return envlist1


def read_xray_nc(file_path, output_file=None):
    """
    Read xrsb_flux data from multiple .nc files, calculate hourly averages, and save results.

    Parameters:
        file_paths (list): List containing paths to multiple .nc files.
        output_file (str): CSV file path to save results.

    Returns:
        dict: Contains daily hourly averages for each file, with file paths as keys and corresponding hourly average lists as values.
    """
    all_results = []

    # for file_path in file_paths:
    try:
        # Open .nc file
        with nc.Dataset(file_path) as f_w:
            # Check if variable exists
            if 'xrsb_flux' not in f_w.variables:
                print(f"Variable 'xrsb_flux' does not exist in file {file_path}, skipping.")

            # Extract date part
            date = extract_date_from_filename(file_path)

            # Extract variables and calculate hourly averages
            for i in range(24):  # Assuming 60 minutes per hour, total 24 hours
                Xray_1h = f_w.variables['xrsb_flux'][i * 60:(i + 1) * 60]
                Xray_1h = np.array(Xray_1h)
                Xray_1h_mean = Xray_1h.mean() if len(Xray_1h) > 0 else np.nan

                # Save hourly data
                all_results.append([f"{date} {i:02d}:00", Xray_1h_mean])

            print(f"Successfully read file {file_path}, hourly averages extracted.")

    except Exception as e:
        print(f"Error reading file {file_path}: {e}")

    # Save results to CSV file
    try:
        if output_file!=None:
            with open(output_file, 'w', newline='') as csvfile:
                csv_writer = csv.writer(csvfile)
                # Write header row
                csv_writer.writerow(["Datetime", "Value"])
                # Write results for each file
                
            print(f"All results saved to {output_file}")
    except Exception as e:
        print(f"Error saving results to file: {e}")
    all_results=np.array(all_results)
    return all_results[:,1:]
def ReadDst(file_path):

    dst_file=open(file_path,"r")
    # This is an lst file containing only Dst data
    dst=dst_file.readlines()
    Dstlist=list()

    for i in range(len(dst)):
        Dst_1h=dst[i].split()
        Dst_1h_result=Dst_1h[-1]
        Dstlist.append(Dst_1h_result)
    Dstlist=np.array(Dstlist)
    Dstlist=Dstlist.reshape((-1,1))
    return Dstlist

if __name__=="__main__":


    # solar_file_path=r"Data\SolarAndGeomagneticData.txt"
    # Dst_data=r"Data\omni2_Sy9V1e8l7m.lst"
        
    # solar=sepc_file(solar_file_path,"201201010000","202408222300")
    # Dst=ReadDst(Dst_data)
    # Dst=Dst[:solar.shape[0]]
    # SolarAndDst=np.concatenate((solar,Dst),axis=1)
    # column_names = ['DateTime', 'F10_7', 'SSN', 'AP', 'Kp', 'Wind_low', 'Wind_high', 'C', 'M', 'X','Dst']
    # df = pd.DataFrame(SolarAndDst, columns=column_names)
    # df.to_csv('test_env.csv', index=False)

    xraydat=read_xray_nc("/home/omnisky/Data_Folder1/ForWork/Data/20240102/Xray/sci_xrsf-l2-avg1m_g16_d20240102_v2-2-0.nc")
    xraydat=np.array(xraydat)
    print(xraydat.shape)